package com.zone.vehicle.superApps.view.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

//import android.os.Handler;
//import android.os.Message;
//import androidx.annotation.NonNull;
//import com.alibaba.fastjson.JSON;
//import com.zone.lib.download.DownloadHelper;
//import com.zone.lib.download.DownloadStatus;
//import com.zone.lib.download.db.DownloadDaoUtilsStore;
//import com.zone.lib.download.db.entity.Download;
//import com.zone.lib.download.utils.LogUtils;
//import com.zone.soa.digitalzonestore.DigitalZoneStoreServiceStub;
//import com.zone.soa.digitalzonestore.IDigitalZoneStoreService;
//import com.zone.soa.digitalzonestore.datatypes.APPContinueInfoOutput;
//import com.zone.soa.digitalzonestore.datatypes.AppDownloadInfoOutput;
//import com.zone.soa.digitalzonestore.datatypes.AppInfoStruct;
//import com.zone.soa.digitalzonestore.datatypes.AppPauseInfoOutput;
//import com.zone.soa.digitalzonestore.datatypes.AppUninstallInfoOutput;
//import com.zone.soa.digitalzonestore.datatypes.AppUpdateInfoOutput;
//import org.apache.commons.lang3.ObjectUtils;
//import java.io.File;
//import java.util.Date;

public class SupperService extends Service {

    private static final String TAG = SupperService.class.getSimpleName();

    private boolean threadFlag;
    private MyThread mThread;

    public SupperService() {
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand");
        startThread();
//        initIDigitalZoneStoreService();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
//        stopOfferService();
    }

    private void startThread(){
        threadFlag = true;
        mThread = new MyThread();
        mThread.start();
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    public class MyThread extends Thread {
        @Override
        public void run() {
            super.run();
            // TODO runtime
            while( threadFlag ) {

//                Log.d(TAG, "Thread pulse");
                try{
                    Thread.sleep(10000);
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        }
    }

    /************************************ICM**************************************************/
//
//    Handler handler = new Handler(new Handler.Callback() {
//        @Override
//        public boolean handleMessage(@NonNull Message msg) {
//
//            Log.d(TAG, msg.toString());
//            switch (msg.what){
//                case 1:
//                    AppInfoStruct appInfoStruct = (AppInfoStruct)msg.obj;
//                    downLoad(appInfoStruct);
//                    break;
//            }
//            return false;
//        }
//    });
//
//
//    private void downLoad(AppInfoStruct appInfoStruct){
//        Log.d(TAG, "download appInfoStruct="+JSON.toJSONString(appInfoStruct));
//        File dir = new File(getApplication().getCacheDir(), "download");
//        if (!dir.exists()){
//            dir.mkdirs();
//        }
//        File saveFile = new File(dir, appInfoStruct.getAppID()+".apk");
//        Download download = DownloadDaoUtilsStore.getInstance().getDownloadByCode(appInfoStruct.getAppID());
//        if (ObjectUtils.isNotEmpty(download)){
//            DownloadDaoUtilsStore.getInstance().deleteByCode(download.getCode());
//            download = null;
//        }
//        if (ObjectUtils.isEmpty(download)){
//            LogUtils.i(TAG, "download database is null");
//            download = new Download();
//            download.setCode(appInfoStruct.getAppID());
//            download.setDownloadStatus(DownloadStatus.PREPARE_DOWNLOAD);
//            download.setDownloadUrl(appInfoStruct.getDownloadURL());
//            download.setFilePath(saveFile.getPath());
//            download.setDownloadLocation(0);
//            download.setType("1");
//            download.setVersion("");
//            download.setCreateTime(new Date().toString());
//            DownloadDaoUtilsStore.getInstance().getDownloadAppDaoUtils().insert(download);
//            DownloadHelper.getInstance().addTask(appInfoStruct.getAppID(), saveFile, download.getDownloadStatus(), 0, "0", appInfoStruct.getVehicleVIN()).submit(this);
//        } else {
//            LogUtils.i(TAG, "download status is "+download.getDownloadStatus());
//            if (download.getDownloadStatus() == DownloadStatus.PAUSE_DOWNLOAD
//                    || download.getDownloadStatus() == DownloadStatus.FAIL_DOWNLOAD){
////                    download.setDownloadStatus(DownloadStatus.PAUSE_DOWNLOAD);
//                LogUtils.i(TAG, "addTask");
//                DownloadHelper.getInstance().addTask(appInfoStruct.getAppID(), saveFile, DownloadStatus.PREPARE_DOWNLOAD, 0, "0", appInfoStruct.getVehicleVIN()).submit(this);
//            }
//        }
//    }
//// TODO 系统接口需要依赖车机环境，正式环境放开
//
//    private MyIDigitalZoneStoreService storeService;
//    private DigitalZoneStoreServiceStub serviceStub;
//
//
//
//    // TODO 系统接口需要依赖车机环境，正式环境放开
//    protected void initIDigitalZoneStoreService() {
//        Log.d(TAG, "initIDigitalZoneStoreService");
//        storeService = new MyIDigitalZoneStoreService();
//        serviceStub = new DigitalZoneStoreServiceStub(storeService, DigitalZoneStoreServiceStub.INSTANCE_NAME_1, "/vendor/etc/sovp/com.zone.soa.digitalzonestoreskeleton");
//        serviceStub.offerService();
//    }
//
//    protected void stopOfferService() {
//        if (serviceStub != null){
//            Log.d(TAG, "stopOfferService");
//            serviceStub.stopOfferService();
//        }
//    }
//
//    // TODO 系统接口需要依赖车机环境，正式环境放开
//    class MyIDigitalZoneStoreService implements IDigitalZoneStoreService {
//
//        @Override
//        public APPContinueInfoOutput aPPContinueInfo(AppInfoStruct appInfoStruct) {
//            Log.d(TAG, "aPPContinueInfo="+ JSON.toJSONString(appInfoStruct));
//            APPContinueInfoOutput output = new APPContinueInfoOutput();
//            if (ObjectUtils.isNotEmpty(appInfoStruct)){
//                output.respCode = "SUCCESS";
//
//            }else {
//                output.respCode = "FAILED";
//
//            }
//            return output;
//        }
//
//        @Override
//        public AppDownloadInfoOutput appDownloadInfo(AppInfoStruct appInfoStruct) {
//            Log.d(TAG, "appDownloadInfo===="+ JSON.toJSONString(appInfoStruct));
//            Message message = new Message();
//            message.what = 1;
//            message.obj = appInfoStruct;
//            handler.sendMessage(message);
//
//            AppDownloadInfoOutput output = new AppDownloadInfoOutput();
//            output.respCode = "SUCCESS";
//            return output;
//        }
//
//        @Override
//        public AppPauseInfoOutput appPauseInfo(AppInfoStruct appInfoStruct) {
//            Log.d(TAG, "appPauseInfo="+ JSON.toJSONString(appInfoStruct));
//            AppPauseInfoOutput output = new AppPauseInfoOutput();
//            output.respCode = "SUCCESS";
//            return output;
//        }
//
//        @Override
//        public AppUninstallInfoOutput appUninstallInfo(AppInfoStruct appInfoStruct) {
//            Log.d(TAG, "appUninstallInfo="+ JSON.toJSONString(appInfoStruct));
//            AppUninstallInfoOutput output = new AppUninstallInfoOutput();
//            output.respCode = "SUCCESS";
//            return output;
//        }
//
//        @Override
//        public AppUpdateInfoOutput appUpdateInfo(AppInfoStruct appInfoStruct) {
//            Log.d(TAG, "appUpdateInfo="+ JSON.toJSONString(appInfoStruct));
//            AppUpdateInfoOutput output = new AppUpdateInfoOutput();
//            output.respCode = "SUCCESS";
//            return output;
//        }
//    }
}